/* assign library aliases */
libname tax79 'path3';
libname tax84 'path4';
libname tax89 'path5';
libname tax94 'path6';
libname out 'YOUR_FILEPATH';

/* 1979 */
data work.i79 (keep= var20 var4 YEAR var6);
set tax79.file3;
YEAR = '1979';
run;

/* 1984 */
data work.i84 (keep= var20 var4 YEAR var6);
set tax84.file4;
YEAR = '1984';
run;

/* 1989 */
data work.i89 (keep= var20 var4 YEAR var6);
set tax89.file5;
YEAR = '1989';
RENAME var7=var4;
run;

/* 1994 */
data work.i94 (keep= var20 var4 YEAR var6);
set tax94.file6;
var4 = var7;
YEAR = '1994';
run;

/* Combine and analyze */
data work.combined;
set work.i79 work.i84
    work.i89 work.i94;
run;

PROC SQL;
CREATE TABLE out.spouse_table AS 
SELECT
YEAR, 
var4,
COUNT(*) AS Total_records,
SUM(CASE WHEN var6 IS NOT NULL THEN 1 ELSE 0 END) AS Total_PIKs
FROM work.combined
GROUP BY YEAR, var4
;
QUIT;

/*export to CSV*/
proc export data=out.spouse_table
    outfile = 'YOUR_FILEPATH/spouse_table.csv'
    dbms=csv
    replace;
run; 